//
//  OMSearchBar.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/21.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMSearchBar.h"
#import "OMTextField.h"
@interface OMSearchBar ()<UITextFieldDelegate>
@property (nonatomic, strong) OMTextField *textField;
@property (nonatomic, strong) UIButton *cancelButton;

@end


@implementation OMSearchBar

- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)configure
{
    _textField = [OMTextField new];
    _textField.font = SM_CFont(31);
    _textField.enablesReturnKeyAutomatically = YES;
    _textField.autocorrectionType = UITextAutocorrectionTypeNo;
    _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _textField.textColor = SM_blackColor;
    _textField.returnKeyType = UIReturnKeySearch;
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.placeholder = @"请您搜索商品";
    _textField.delegate = self;
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.backgroundColor = SM_whiteColor;
    _textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    UIButton *leftButton = [UIButton new];
    [leftButton setImage:[UIImage imageNamed:@"SearchStoreBlack"] forState:UIControlStateNormal];
    [leftButton sizeToFit];
    _textField.leftView = leftButton;
    [self addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(15)));
        make.top.bottom.equalTo(@0);
    }];

    _cancelButton = [UIButton new];
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton setTitleColor:SM_blackColor forState:UIControlStateNormal];
    [_cancelButton.titleLabel setFont:SM_CFont(33)];
    [self addSubview:_cancelButton];
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(0)));
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(_textField.mas_right).offset(SM_SIZE(0));
        make.width.equalTo(@(SM_SIZE(100)));
    }];
    @weakify(self);
    [[_cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self.textField resignFirstResponder];
        if (self.delegate && [self.delegate respondsToSelector:@selector(searchbarDidClickCancelButton:)]) {
            [self endEdit];
            [self.delegate searchbarDidClickCancelButton:self];
        }
    }];

    self.showCancelButton = true;
}

- (void)setShowCancelButton:(BOOL)showCancelButton
{
    _showCancelButton = showCancelButton;
    [_cancelButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(showCancelButton?SM_SIZE(100):0));
    }];
    _cancelButton.hidden = !showCancelButton;
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(!showCancelButton?SM_SIZE(40):0));
    }];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarShouldClear:)]) {
        [self.delegate searchBarShouldClear:self];
    }
    return true;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBar:shouldChangeText:)]) {
        [self.delegate searchBar:self shouldChangeText:string];
    }
    return true;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        return [self.delegate searchBarShouldBeginEditing:self];
    }
    return true;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self returnAction];
    return false;
}



#pragma mark - Getters
- (NSString *)text
{
    return _textField.text;
}

#pragma mark - Setters
 - (void)setText:(NSString *)text
{
    _textField.text = text;
    [self returnAction];
}


#pragma mark - Actions 
- (void)returnAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarShouldReturn:)]) {
        [self.delegate searchBarShouldReturn:self];
    }
    [self endEdit];
}

- (void)startEdit
{
    [self.textField becomeFirstResponder];
}
- (void)endEdit
{
    [self.textField resignFirstResponder];
}
@end
